﻿using UnityEngine;

namespace Assets.Custom_Scripts
{
    public class GiftController : MonoBehaviour
    {

        protected bool _grabbed = false;

        public Vector3 GrabbedPosition;
        public GameObject CurrentOwner;
        public Transform CharacterHand;

        // Use this for initialization
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {
            #region Add this gift as a child game object to the person who collected it.

            if (_grabbed)
            {

                transform.parent = CurrentOwner.transform;
                transform.position = GrabbedPosition + CurrentOwner.transform.position;


            }

            #endregion Add this gift as a child game object to the person who collected it.

        }

        public void OnTriggerEnter(Collider character)
        {
            Debug.Log(@"Collided with " + character.gameObject.tag);

            if (_grabbed)
            {
                return;
            }

            #region Increment flower count with relationship controller of character:

            if (null != character)
            {

                if (!character.gameObject.tag.Contains(@"Player"))
                {
                    return;
                }

                if (!_grabbed)
                {
                    _grabbed = true;

                    if (gameObject.tag.Contains(@"Flowers"))
                    {
                        character.SendMessage("AcquireFlowers", gameObject);
                    }

                    CurrentOwner = character.gameObject;
                }
                else
                {
                    if (character.gameObject != CurrentOwner)
                    {
                        RelationshipController relationshipsOfCurrentOwner = CurrentOwner.gameObject.GetComponent<RelationshipController>();

                        for (int i = 0; i < relationshipsOfCurrentOwner.PotentialLoveInterests.Length; i++)
                        {
                            if (relationshipsOfCurrentOwner.PotentialLoveInterests[i] == character.gameObject)
                            {
                                if (gameObject.tag.Contains(@"Flowers"))
                                {
                                    character.gameObject.SendMessage(@"ReceiveFlowersFrom", CurrentOwner);
                                }
                                Destroy(gameObject);
                                return;
                            }
                        }
                    }
                }
            }

            #endregion Increment flower count with relationship controller of character:

            //Destroy(this.gameObject);

        }

        public void FixedUpdate()
        {

        }

        public void GiveGift()
        {
            Destroy(gameObject);
        }

    }

}